package com.lc1993929.tank.controller;

import com.lc1993929.tank.constants.Condition;
import com.lc1993929.tank.entity.requestvo.Init;
import com.lc1993929.tank.entity.vo.TankResult;
import com.lc1993929.tank.service.GlobalService;
import com.lc1993929.tank.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LiuChang
 * @date 2018/11/24 19:04
 */
@RequestMapping("/player")
@RestController
public class TankController {

    @Autowired
    private GlobalService globalService;


    @PostMapping("/init")
    public void tank(@RequestBody Init init) {
        globalService.init(init);
        globalService.startCountDown();
    }

    @PostMapping("/action")
    public List<TankResult> action(@RequestBody Init init) {
        /*ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(init));
        init.getView().getMap().forEach(mapEnums -> {
            mapEnums.forEach(mapEnum -> System.out.print(mapEnum + "   "));
            System.out.println();
        });*/
        List<TankResult> tankResults;
        try {
            globalService.refreshAll(init);
            tankResults = globalService.doAction();
        } catch (Exception e) {
            tankResults = new ArrayList<>();
            e.printStackTrace();
            TankResult.Type myWZ123RandomResultType = Util.getRandomResultType();
            TankResult.TankResultBuilder myWZ123TankResultBuilder = TankResult.builder().tId(Condition.myWZ123.tId).direction(Util.getRandomDirection()).type(myWZ123RandomResultType).useGlod(false);
            if (myWZ123RandomResultType == TankResult.Type.MOVE) {
                myWZ123TankResultBuilder.length(1);
            } else {
                myWZ123TankResultBuilder.length(3);
            }
            tankResults.add(myWZ123TankResultBuilder.build());
            TankResult.Type myArmata1RandomResultType = Util.getRandomResultType();
            TankResult.TankResultBuilder myArmata1TankResultBuilder = TankResult.builder().tId(Condition.myArmata1.tId).direction(Util.getRandomDirection()).type(myArmata1RandomResultType).useGlod(false);
            if (myArmata1RandomResultType == TankResult.Type.MOVE) {
                myArmata1TankResultBuilder.length(2);
            } else {
                myArmata1TankResultBuilder.length(1);
            }
            tankResults.add(myArmata1TankResultBuilder.build());
            TankResult.Type myArmata2RandomResultType = Util.getRandomResultType();
            TankResult.TankResultBuilder myArmata2TankResultBuilder = TankResult.builder().tId(Condition.myArmata2.tId).direction(Util.getRandomDirection()).type(myArmata2RandomResultType).useGlod(false);
            if (myArmata2RandomResultType == TankResult.Type.MOVE) {
                myArmata2TankResultBuilder.length(2);
            } else {
                myArmata2TankResultBuilder.length(1);
            }
            tankResults.add(myArmata2TankResultBuilder.build());
            TankResult.Type myK2PantherRandomResultType = Util.getRandomResultType();
            TankResult.TankResultBuilder myK2PantherTankResultBuilder = TankResult.builder().tId(Condition.myK2Panther.tId).direction(Util.getRandomDirection()).type(myK2PantherRandomResultType).length(1).useGlod(false);
            tankResults.add(myK2PantherTankResultBuilder.build());
            TankResult.Type myT90RandomResultType = Util.getRandomResultType();
            TankResult.TankResultBuilder myT90TankResultBuilder = TankResult.builder().tId(Condition.myT90.tId).direction(Util.getRandomDirection()).type(myT90RandomResultType).length(1).useGlod(false);
            tankResults.add(myT90TankResultBuilder.build());
        }
       /* tankResults.forEach(tankResult -> {
            System.out.println(tankResult.gettId() + "-" + tankResult.getType() + "-" + tankResult.getDirection() + "-" + tankResult.getLength());
        });*/
        return tankResults;
    }

}
